/*
 *@本js功能：在线批阅详情
 *@本js作者：杨锡君
 *@编写时间：2017年11月23日
 */
$(function(){
    var Alert1 = new Alert();
    var confirm1 = new Confirm();
    var loading1 = new Loading();
    loading1.init();
    var $classAndName = $(".classAndName");
    var onlinequestionId;
    function examInfo(opt){
        var choseNum = 0;
        if($(".totalChoseCount").length!=0){
            choseNum = $(".totalChoseCount").html();
        }
        var muchNum = 0;
        if($(".totalMuchCount").length!=0){
            muchNum = $(".totalMuchCount").html();
        }
        var checkNum = 0;
        if($(".totalCheckCount").length!=0){
            checkNum = $(".totalCheckCount").html();
        }
        var	fillNum = 0;
        if($(".totalFillCount").length!=0){
            fillNum = $(".totalFillCount").html();
        }
        var	shortNum = 0;
        if($(".totalShortCount").length!=0){
            shortNum = $(".totalShortCount").html();
        }
        var opworcount = opt.opworcount;
        var manyOpworcount = opt.manyOpworcount;
        if(opt.submitMark == 1){
            opworcount = choseNum;
            manyOpworcount = muchNum;
        }
        var str = "<div style=\"width: 500px;height: auto;\" class=\"inputRight\"><label >试卷科目:&ensp;</label><span class=\"courseName\">"+ result.paramData.courseName +"</span></div>"
            +"<div class=\"fl inputRights\"><label class=\"fl\">单&ensp;选&ensp;题:&ensp;</label><span class=\"choseTotal\">共"+ choseNum +"题</span><span class=\"choseTotal\">得分<a class=\"choseGetScore\" href=\"javascript:;\">"+ opt.optionallsource +"</a>分</span><span class=\"choseTrueNum\">正确"+ (Number(choseNum)-opworcount) +"题</span><span class=\"choseFalseNum\">错误"+ opworcount +"题</span></div>"
            +"<div class=\"clear\"></div>"
            +"<div class=\"fl inputRights\"><label class=\"fl\">多&ensp;选&ensp;题:&ensp;</label><span class=\"choseTotal\">共"+ muchNum +"题</span><span class=\"choseTotal\">得分<a class=\"muchGetScore\" href=\"javascript:;\">"+ opt.manyOptionallsource +"</a>分</span><span class=\"choseTrueNum\">正确"+ (Number(muchNum)-manyOpworcount) +"题</span><span class=\"choseFalseNum\">错误"+manyOpworcount+"题</span></div>"
            +"<div class=\"clear\"></div>"
            +"<div class=\"fl inputRights\"><label class=\"fl\">判&ensp;断&ensp;题:&ensp;</label><span class=\"choseTotal\">共"+ checkNum +"题</span><span class=\"choseTotal\">得分<a class=\"checkGetScore\" href=\"javascript:;\">"+ opt.judgeallsource +"</a>分</span><span class=\"choseTrueNum\">正确"+ (Number(checkNum)-opt.judworcount) +"题</span><span class=\"choseFalseNum\">错误"+opt.judworcount+"题</span></div>"
            +"<div class=\"clear\"></div>"
            +"<div class=\"fl inputRights\"><label class=\"fl\">填&ensp;空&ensp;题:&ensp;</label><span class=\"choseTotal\">共"+ fillNum +"题</span><span class=\"choseTotal\">得分<a class=\"fillGetScore\" href=\"javascript:;\">"+ opt.blanksallsource +"</a>分</span></div>"
            +"<div class=\"clear\"></div>"
            +"<div class=\"fl inputRights\"><label class=\"fl\">简&ensp;答&ensp;题:&ensp;</label><span class=\"choseTotal\">共"+ shortNum +"题</span><span class=\"choseTotal\">得分<a class=\"shortGetScore\" href=\"javascript:;\">"+ opt.answersource +"</a>分</span></div>"
            +"<div class=\"clear\"></div>"
            +"<div class=\"fl inputRights\"><label class=\"fl\">总&ensp;分&ensp;数:&ensp;</label><a class=\"totalGetScore\" href=\"javascript:;\">"+ (opt.judgeallsource*10 +opt.optionallsource*10 +opt.manyOptionallsource*10 + opt.blanksallsource*10 + opt.answersource*10)/10 +"</a>分</span></div>"
            +"<div class=\"clear\"></div>";
        return str;
    }
    var testReviewDetail = {
        downHtml:'',
        init:function(){
            this.bindEvent();
        },
        bindEvent:function(){
            var self = this;
            var sumScore = self.getTextEvent();
            $(".submitScore").off("click").click(function(){
                var tolScore =$(".totalGetScore").html();
                if(tolScore == undefined){
                    tolScore = 0;
                }
                if(Number(tolScore)>Number($(".submitScore").attr("data-sum"))){
                    Alert1.content = "批阅分数不能大于试卷总分数，当前试卷分数为"+$(".submitScore").attr("data-sum");
                    Alert1.init();
                    Alert1.show();
                    return false;
                }else{
                    self.submitEvent(sumScore);
                }

            });
        },
        getTextEvent:function(){
            var self = this;
            var sumScore = 0;
            var jsonStr = JSON.stringify({
                "courseId":result.paramData.courseId,
                "stuorteaId":result.paramData.stuorteaId,
                "onlinequestionId":result.paramData.id
            });
            loading1.show();
            $.ajax({
                type:"post",
                url:"markOnlineExam",
                async:true,
                data:{
                    "jsonStr":jsonStr
                },
                success:function(msg){
                    loading1.hide();
                    msg = JSON.parse(msg);
                    if(msg.errMsg == null){
                        if(msg.question != null){
                            $(".submitScore").attr("data-sum",msg.question.onlineSumSource);
                            var text = unescape(msg.question.oTestJson);
                            text = text.replace(/\(\+\)/g,"(<b class=\"answerInt\" ></b>)");
                            text = text.replace(/\(\&emsp\;\&emsp\;\)/g,"(<b class=\"answerInt\" ></b>)");
                            $(".titleHeads").after(text);
                            $(".questionBox[data-questiontype=3]").find(".trueAnswer").show();
                            $(".questionBox[data-questiontype=4]").find(".trueAnswer").show();
                            $.map(msg.question.answerList, function(opt) {
                                var arr = opt.ansquesContent.split(",");
                                $.each($(".questionBox[data-questionid="+ opt.questionId +"]").find(".answerInt"), function(key) {
                                    $(this).html(arr[$(this).index()]);
                                });
                            });
                            // return msg.question.onlineSumSource;
                            $.each( $(".shortAnswer"),function (opt) {
                                $(this).after("<b style='padding: 0;font-weight: normal;' class='shortAnswer'>"+ $(this).val() +"</b>");
                                $(this).before("<b style='font-weight: normal' class='stuAns'>学生答案：</b>");
                                $(this).remove();
                            });

                            $(".titleHeads>div").html(result.paramData.courseName);
                            $(".classAndName").html(result.paramData.className+ "--" + result.paramData.userName+".");
                            var str = examInfo(msg.question);
                            $(".titleHead").after(str);
                            self.downHtml = escape($(".downBox").html())
                            $(".questionBox[data-questiontype=3]").append("<i class='getDisplay'>得分：<input maxlength='4' type='text' class='getScoreInt'><i>");
                            $(".questionBox[data-questiontype=4]").append("<i class='getDisplay'>得分：<input maxlength='4' type='text' class='getScoreInt'><i>");
                            maxLengthLimit.init("getScoreInt");
                            onlinequestionId = msg.question.onlinequestionId;
                            if(msg.question.submitMark == 1){
                                $(".trueAnswer").show()
                            }else{
                                $.each(msg.question.errList,function (key) {
                                    $(".questionBox[data-questionid="+  this +"]").find(".trueAnswer").show().end().find(".answerInt").css("color","#c80211");
                                });
                            }
                            if(msg.question.scoreList !=undefined){
                                var arr = msg.question.scoreList.split(",");
                                for(var i = 0;i<$(".getScoreInt").length;i++){
                                    $(".getScoreInt").eq(i).val(arr[i]);
                                }
                            }

                            self.verificationEvent();
                        }

                    }else{
                        Alert1.content = msg.errMsg;
                        Alert1.success = function () {
                            var json = JSON.stringify({
                                "pageName":"testReviewMana"
                            });
                            window.location.href = "skipPage?jsonStr="+json;
                        };
                        Alert1.init();
                        Alert1.show();
                    }
                }
            });
        },
        submitEvent:function(){
            var self = this;
            $(".popup,.mb").show();
            var totalScore = $(".totalGetScore").html();
            if(totalScore == undefined){
                totalScore = 0;
            }
            $(".stuGetScroe").html(totalScore+"分");
            $(".male").off("click").on("click", function() {
                $(this).removeClass("radio").addClass("radioCheck");
                $(".female").removeClass("radioCheck").addClass("radio");
            });
            $(".female").off("click").on("click", function() {
                $(this).removeClass("radio").addClass("radioCheck");
                $(".male").removeClass("radioCheck").addClass("radio");
            });
            //取消和X事件
            $(".popup_img").off("click").on("click", function() {
                $(".mb,.popup").hide();
            });
            $(".exportCancle").off("click").on("click", function() {
                $(".mb,.popup").hide();
            });
            $(".exportBtn").off("click").click(function(){
                var arr = [];
                for(var i = 0;i<$(".getScoreInt").length;i++){
                    arr.push($(".getScoreInt").eq(i).val());
                }
                var jsonStr ={
                    "answercount":0,
                    "answersource":Number($(".shortGetScore").html()),
                    "blanksallsource":Number($(".fillGetScore").html()),
                    "blankscount":0,
                    "classId":result.paramData.classId,
                    "onlinequestionId":onlinequestionId,
                    "courseId":result.paramData.courseId,
                    "stuorteaId":result.paramData.stuorteaId,
                    "scoreList":arr.join(),
                    "cssStr":'@charset "utf-8";button:focus,input,input:focus,select,textarea:focus{outline:0}body,dd,dl,dt,form,h1,h2,h3,h4,h5,h6,input,li,ol,p,textarea,ul{margin:0;padding:0}select::-ms-expand{display:none;}select{border:1px solid #dcd9d9;appearance:none;-moz-appearance:none;-webkit-appearance:none;background:url(../../images/select.gif) right 10px center no-repeat #FFF}body,html{width:100%}label{font-size:16px!important;}.popup_content{font-size:16px!important;}body{font-size:16px;line-height:1.7;font-family:"Microsoft YaHei",simhei;color:#333;background-color:#DFDFDF;}input,select,textarea{color:#333;outline:none;}input{border:1px solid #dfdfdf;}input[type=checkbox]{-webkit-appearance:none;appearance:none;width:14px;vertical-align:middle;height:14px;margin:0;cursor:pointer;background:#fff;border:1px solid #a0a0a0;-webkit-border-radius:1px;-moz-border-radius:1px;border-radius:3px;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;position:relative;}input[type=checkbox]:active,input[type=radio]:active{border-color:#a0a0a0;background:#fff;}input[type=checkbox]:hover{border-color:#a0a0a0;-webkit-box-shadow:inset 0 2px 2px rgba(0,0,0,0.1);-moz-box-shadow:inset 0 2px 2px rgba(0,0,0,0.1);box-shadow:inset 0 2px 2px rgba(0,0,0,0.1);}input[type=checkbox]:checked,input[type=radio]:checked{background:#fff;}input[type=checkbox]:checked::after{content:url(../../images/ico_ok.png);display:block;position:absolute;top:-2px;left:2px;}input[type=checkbox]:focus{outline:none;border-color:#a0a0a0;}input[type=text]:focus{outline:none;}input:-webkit-autofill{-webkit-box-shadow:0 0 0px 1000px white inset;}ul,ol{list-style:none;}a{text-decoration:none;color:#333;font-size:16px}a:visited{color:#333;}a:hover,a:active,a:focus{color:#333;}em,i{font-style:normal}b,strong{font-weight:normal;}.fl{float:left}.fr{float:right}.clear{clear:both;height:0;overflow:hidden;}img{font-size:0;display:block;overflow:hidden;}table{table-layout:fixed;}td{white-space:nowrap;overflow:hidden;text-overflow:ellipsis;}::-webkit-input-placeholder{color:#999999;}:-moz-placeholder{color:#999999;}::-moz-placeholder{color:#999999;}:-ms-input-placeholder{color:#999999;}.masking,.mb{position:fixed;width:100%;height:100%;z-index:0;background:rgba(0,0,0,0.7);top:0;left:0;display:none;}@charset "utf-8";body{background:none;}.titleHead,.titleHeads{width:100%;height:35px;position:relative;border-bottom:1px solid #c9c9c9;}.titleHead span{color:#333;line-height:16px;display:block;}.titleHead b{font-weight:bolder;}.titleHeads div{width:500px;height:100%;text-align:center;font-size:16px;margin:0 auto;color:#333;line-height:16px;}.inputRight{margin-top:10px;width:180px;height:27px;line-height:27px;margin-bottom:0;}.inputRight label,.inputRights label{font-weight:bolder;}.inputRights{margin-top:10px;height:27px;line-height:27px;margin-bottom:0;}.inputRights span{display:block;width:100px;height:27px;line-height:27px;margin-right:20px;float:left;}.inputRights a{text-decoration:underline;cursor:default;color:#C80211;}.inputRights a:hover{color:#C80211;}.inputRights .tip{text-decoration:none;}.inputBottom{margin-bottom:30px;}.mainContents{padding:22px 25px 50px;width:auto;margin-top:10px;background:#fff;}.submitScore{position:fixed;bottom:0;left:50%;margin-left:-50px;}.popup{position:fixed;display:none;}span.male,span.female{width:42px;display:inline-block;text-align:right;cursor:pointer;margin-right:30px;}.radioCheck{background:url(../../images/ico_radioCheck.png) no-repeat left  center;}.radio{background:url(../../images/ico_radio.png) no-repeat left center;}.popup_btn .popup_close:hover{margin-left:50px;background:none;border:1px solid #c80211;color:#c80211;}.popup_content div{margin:10px auto;width:245px;}.previewBox{width:98%;left:1%;top:2%;height:96%;overflow:auto;}.titleImg{display:inline-block;margin-bottom:-4px;}.popup_content{height:auto;font-size:16px;padding:20px;position:relative;}.examPageTitle{font-weight:bolder;}.questionBox{padding:0 32px 10px;}.choseOne{padding:0 15px;clear:both;}.trueAnswer{margin-left:25px;color:#c80211;display:none;}.shortAnswer{width:50%;height:130px;resize:none;font-size:16px;padding:20px;background:#fff;display:inline-block;}.downloadExam{position:fixed;bottom:0;left:50%;margin-left:-50px;}.getScoreInt{height:20px;width:50px;text-align:center;}.stuAns{display:inline-block;width:85px;font-size:16px;float:left;margin-left:9px;}.answerInt{word-break:break-all;}h4 b{font-weight:bolder;}',
                    "htmlStr":self.downHtml
                };
                if($(".radioCheck").attr("data-value") == 1){
                    jsonStr.examesource =	Number(totalScore);
                    jsonStr.examMark = 1;
                }else{
                    jsonStr.examesource = 0;
                    jsonStr.examMark = 0;
                }
                $.ajax({
                    type:"post",
                    url:"isExamSource",
                    async:true,
                    data:{
                        "jsonStr":JSON.stringify(jsonStr)
                    },
                    success:function(msg){
                        msg = JSON.parse(msg);
                        if(msg.resultMark == "success"){
                            Alert1.content ="批阅试卷成功";
                            Alert1.success = function(){
                                var json = JSON.stringify({
                                    "pageName": "testReviewMana"
                                });
                                window.location.href = "skipPage?jsonStr=" + json;
                            };
                            Alert1.init();
                            Alert1.show();
                        }else{
                            Alert1.content = msg.errMsg;
                            Alert1.init();
                            Alert1.show();
                        }
                    }
                });

            });
        },
        verificationEvent: function() {
            //			输入框校验
            $(".getScoreInt").off("blur").blur(function() {
                var flag = verification.numbersCode($(this).val());
                if($(this).val()!=""){
                    if(flag != true) {
                        Alert1.content =  flag;
                        $(this).val("");
                        Alert1.init();
                        Alert1.show();
                    }
                    if($(this).parent().parent().attr("data-questiontype") == 4){
                        var score = Number($(this).val());
                        if(score<0||score>$(".oneShortScore").html().substring(0,$(".oneShortScore").html().indexOf("分"))){
                            Alert1.content =  "正确输入每题分数";
                            $(this).val("");
                            Alert1.init();
                            Alert1.show();
                        }

                    }
                    if($(this).parent().parent().attr("data-questiontype") == 3){
                        var score = Number($(this).val());
                        if(score<0||score>$(".oneFillScore").html().substring(0,$(".oneFillScore").html().indexOf("分"))){
                            Alert1.content =  "正确输入每题分数";
                            $(this).val("");
                            Alert1.init();
                            Alert1.show();
                        }

                    }
                }
                // 分值赋值
                var fill = 0;
                var short = 0;
                $.each($(".getScoreInt"),function (key) {
                    if($(this).parent().parent().attr("data-questiontype") == 3){
                        var score = $(this).val();
                        if(score == ""){
                            score = 0;
                        }
                        fill+=Number(score);
                    }
                    if($(this).parent().parent().attr("data-questiontype") == 4){
                        var scores = $(this).val();
                        if(scores == ""){
                            scores = 0;
                        }
                        short += Number(scores);
                    }
                });
                $(".fillGetScore").html(fill);
                $(".shortGetScore").html(short);

                // if($(this).parent().parent().attr("data-questiontype") == 4){
                //     $(".shortGetScore").html(Number($(".shortGetScore").html())+Number($(this).val()));
                // }checkGetScore
                $(".totalGetScore").html((Number($(".checkGetScore").html())*10+Number($(".choseGetScore").html())*10+Number($(".muchGetScore").html())*10+Number($(".fillGetScore").html())*10+Number($(".shortGetScore").html())*10)/10)

            });
        }
    };
    testReviewDetail.init();
});
